package com.wanxi.dao.impl;

import com.wanxi.dao.ProductDao;
import com.wanxi.dto.ProductDto;
import com.wanxi.model.ProductModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product (" +
                "name,price,normal_price,img_href," +
                "enable,content,cuisine_id,create_time) values('"+productModel.getName()+"'" +
                ",'"+productModel.getMarketPrice()+"'" +
                ",'"+productModel.getNormalPrice()+"'," +
                "'"+productModel.getImgHref()+"'" +
                ",'"+productModel.getEnable()+"'" +
                ",'"+productModel.getContent()+"'" +
                ",'"+productModel.getCuisineId()+"',now())";
        return JDBC.update(sql);
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "";
        return JDBC.update(sql);
    }

    @Override
    public int delete(ProductModel productModel) {
        String sql = "";
        return JDBC.update(sql);
    }

    @Override
    public ProductDto findById(ProductModel productModel) {
        String sql = "select * from product where id=" + productModel.getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return ProductDto.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .marketPrice(query.getBigDecimal("price"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .imgHref(query.getString("img_href"))
                        .content(query.getString("content"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<ProductDto> findAll(ProductModel model) {
        String sql = "select * from (" +
                "select p.*,c.name  cuisine_name from product p  left join cuisine c  on p.cuisine_id=c.id " +
                ") temp where true ";
        sql += getSql(model);
        sql+=" order by id desc ";
        sql += " limit " + (model.getPage() - 1) * model.getLimit() + "," + model.getLimit();
        ResultSet query = JDBC.query(sql);
        List<ProductDto> list = new ArrayList<>();
        try {
            while (query.next()) {
                list.add(ProductDto.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .marketPrice(query.getBigDecimal("price"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .imgHref(query.getString("img_href"))
                        .content(query.getString("content"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .cuisineName(query.getString("cuisine_name"))
                        .build());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String getSql(ProductModel model) {
        String sql = "";
        if (model.getName() != null) {
            sql += " and name like '%" + model.getName() + "%'";
        }
        if (model.getEnable() != -1) {
            sql += " and enable =" + model.getEnable();
        }
        if (model.getStartCreateTime() != null) {
            sql += " and create_time >='" + model.getStartCreateTime() + "' ";
        }
        if (model.getEndCreateTime() != null) {
            sql += " and create_time <='" + model.getEndCreateTime() + "'";
        }
        return sql;
    }

    @Override
    public int count(ProductModel model) {
        String sql = "select count(*) count from ( " +
                "select p.*,c.name  cuisine_name from product p  left join cuisine c  on p.cuisine_id=c.id  " +
                ") temp where true ";
        sql += getSql(model);
        return JDBC.count(sql);
    }
}
